
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.*;

/**
 * @author GongXincheng
 * @since 2019-08-02 14:02
 */
public class Demo1 {
    public static void main(String[] args) throws Exception {
        nioCopyFile("/Users/gxc/My_Tmp/hello.txt", "/Users/gxc/My_Tmp/head_3.png");
    }

    public static void nioCopyFile(String resource, String destination) throws Exception {

        FileInputStream inputStream = new FileInputStream(resource);
        FileOutputStream outputStream = new FileOutputStream(destination);

        FileChannel readChannel = inputStream.getChannel();
        FileChannel writeChannel = outputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        int len;
        while ((len = readChannel.read(buffer)) != -1) {
            buffer.flip();
//            writeChannel.write(buffer);
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }
            buffer.clear();
        }

        /*while (true) {

            buffer.clear();
            int len = readChannel.read(buffer);
            if (len == -1) {
                break;
            }
            buffer.flip();
            writeChannel.write(buffer);
        }*/

        writeChannel.close();
        readChannel.close();
    }


    @Test
    public void test1() throws InterruptedException {
        /*ThreadPoolExecutor executor =
                new ThreadPoolExecutor(1, 1, 0L,
                        TimeUnit.MINUTES, new LinkedBlockingQueue<>());

        for (int i = 0; i < 10; i++) {
            executor.execute(() -> {
                for (int y = 0; y < 100; y++) {
                    System.out.println(Thread.currentThread().getName() + "____" + y);
                }
            });
        }*/

        for (int i = 0; i < 1000; i++) {
            System.out.println("hello");
        }

        byte[] arr = new byte[6 * 1024 * 1024];

        System.out.println("CPU数量：" + Runtime.getRuntime().availableProcessors());
    }
}
